package com.danger.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.widget.AppCompatSeekBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.danger.model.entity.Song;
import com.danger.service.PlayServiceImpl;
import com.danger.util.Constant;
import com.danger.widget.MarqueeTextView;
import com.danger.widget.MyCircleImageView;

import java.util.List;

/**
 * Created by 俊 on 2016/8/17.
 */
public class PlayActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout root_layout;
    private Toolbar player_toolbar;
    private MarqueeTextView name_textView;
    private MarqueeTextView artist_textView;
    private MyCircleImageView photo_imageView;
    private TextView left_textView;
    private AppCompatSeekBar seekBar;
    private TextView right_textView;
    private ImageButton previous_song_button;
    private ImageButton play_song_button;
    private ImageButton next_song_button;
    private MyReceiver receiver;
    private List<Song> songs;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.player_layout);

        bindView();
        bindEvent();
    }

    @Override
    public void bindView() {
        root_layout = (RelativeLayout) findViewById(R.id.root_layout);
        player_toolbar = (Toolbar) findViewById(R.id.player_toolbar);
        name_textView = (MarqueeTextView) findViewById(R.id.name_textView);
        artist_textView = (MarqueeTextView) findViewById(R.id.artist_textView);
        photo_imageView = (MyCircleImageView) findViewById(R.id.photo_imageView);
        left_textView = (TextView) findViewById(R.id.left_textView);
        seekBar = (AppCompatSeekBar) findViewById(R.id.seekBar);
        right_textView = (TextView) findViewById(R.id.right_textView);
        previous_song_button = (ImageButton) findViewById(R.id.previous_song_button);
        play_song_button = (ImageButton) findViewById(R.id.play_song_button);
        next_song_button = (ImageButton) findViewById(R.id.next_song_button);
        receiver = new MyReceiver();
        IntentFilter filter = new IntentFilter(Constant.UPDATE_UI);
        registerReceiver(receiver, filter);
        if (getIntent() != null) {
            
        }
    }

    @Override
    public void bindEvent() {
        player_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        previous_song_button.setOnClickListener(this);
        play_song_button.setOnClickListener(this);
        next_song_button.setOnClickListener(this);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    Intent intent = new Intent(PlayActivity.this, PlayServiceImpl.class);
                    intent.putExtra("progress", progress);
                    intent.setAction(Constant.DRAG_SEEKBAR);
                    startService(intent);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(PlayActivity.this, PlayServiceImpl.class);
        switch (v.getId()) {
            case R.id.previous_song_button:
                intent.setAction(Constant.PREVIOUS_MSG);
                startService(intent);
                break;
            case R.id.play_song_button:
                intent.setAction(Constant.PAUSE_MSG);
                startService(intent);
                break;
            case R.id.next_song_button:
                intent.setAction(Constant.NEXT_MSG);
                startService(intent);
                break;
            default:
                break;
        }
    }

    private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //updateUI(intent);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (receiver != null) {
            unregisterReceiver(receiver);
        }
    }
}
